package com.ysx.article.controller;


import com.ysx.article.dto.ApArticleDto;
import com.ysx.article.dto.ArticleBehaviourDtoQuery;
import com.ysx.article.pojo.ApArticle;
import com.ysx.article.service.ApArticleService;
import com.ysx.common.pojo.PageInfo;
import com.ysx.common.pojo.PageRequestDto;
import com.ysx.common.pojo.Result;
import com.ysx.core.controller.AbstractCoreController;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
* <p>
* 文章信息表，存储已发布的文章 控制器</p>
* @author ljh
* @since 2021-09-08
*/
@Api(value="文章信息表，存储已发布的文章",tags = "ApArticleController")
@RestController
@RequestMapping("/apArticle")
public class ApArticleController extends AbstractCoreController<ApArticle> {

    private ApArticleService apArticleService;

    //注入
    @Autowired
    public ApArticleController(ApArticleService apArticleService) {
        super(apArticleService);
        this.apArticleService=apArticleService;
    }

    //有条件的 分页 排序查询
    @PostMapping("/searchOrder")
    public Result<PageInfo<ApArticle>> searchOrder(@RequestBody PageRequestDto<ApArticle> pageRequestDto){
        //1.获取当前页码 和 每页显示的行
        if (pageRequestDto.getSize()>10) {
            //设置最多为10
            pageRequestDto.setSize(10L);
        }
        if (pageRequestDto.getPage()<=0) {
            //设置为第一个页
            pageRequestDto.setPage(1L);
        }
        //3.执行分页查询
        PageInfo<ApArticle> pageInfo =   apArticleService.pageByOrder(pageRequestDto);
        return Result.ok(pageInfo);
    }



    //展示详细的文章数据
    @GetMapping("/detail/{articleId}")
    public Result<ApArticleDto> detailByArticleId(@PathVariable(name="articleId") Long articleId){
        ApArticleDto articleInfoDto = apArticleService.detailByArticleId(articleId);
        return Result.ok(articleInfoDto);
    }

    //展示文章中的用户行为信息
    @PostMapping("/load/article/behavior")
    public Result<Map<String,Object>> loadArticleBehaviour(@RequestBody ArticleBehaviourDtoQuery articleBehaviourDtoQuery){
        Map<String,Object> resultMap = apArticleService.loadArticleBehaviour(articleBehaviourDtoQuery);
        return Result.ok(resultMap);
    }

    //根据channelId首页加载
    @GetMapping("/loadHomePage/{channelId}")
    public Result<List<ApArticle>> loadMoreFromRedis(@PathVariable(name="channelId")Integer channelId){
        List<ApArticle> list = apArticleService.loadMoreFromRedis(channelId);
        return Result.ok(list);
    }



}

